package com.qf.controller;

import com.qf.pojo.DtsAdmin;
import com.qf.pojo.DtsComment;
import com.qf.service.DtsBrandService;
import com.qf.service.DtsCommentService;
import com.qf.util.ResponseUtil;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author bss
 * @version V1.0
 * @Project maven
 * @Package com.qf.controller
 * @Description:
 * @Date 2022/10/18 15:00
 */
@RestController
@RequestMapping("/admin/comment")
public class AdminCommentController {

    @Autowired
    DtsCommentService commentService;
    @Autowired
    DtsBrandService brandService;

//    comment/list?page=1&limit=20&userId=111&valueId=111&sort=add_time&order=desc
    @GetMapping("/list")
    public Object list(@RequestParam(value = "page",required = false, defaultValue = "1") Integer page,
                       @RequestParam(value = "limit",required = false, defaultValue = "20")Integer limit,
                       @RequestParam(value = "sort",required = false, defaultValue = "add_time")String sort,
                       @RequestParam(value = "order",required = false, defaultValue = "desc")String order,
                       Integer usersId,
                       @RequestParam(value = "valueId",required = false) Integer goodId){


        List<Integer> brandsIds = null;
                //判断：如果品牌制造商，只能查询该管理员对应品牌商品的评论数据
        Subject subject = SecurityUtils.getSubject();
        if (subject.hasRole("品牌制造商")){
            DtsAdmin admin = (DtsAdmin) subject.getPrincipal();
            brandsIds = brandService.findBrandByAdminId(admin.getId());
        }

        Map<String , Object> data = commentService.selectByPage(page,limit,sort,order,usersId,goodId,brandsIds);
        return ResponseUtil.ok(data);

    }

    @PostMapping("/delete")
    public Object delete(@RequestBody DtsComment comment){
        commentService.delete(comment);

        return ResponseUtil.ok();
    }
}
